package com.yuanqi.cmbc.service;

import com.yuanqi.dz.dc.util.DateUtil;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import pnbclient.helper.StringHelper;
import pnbclient.util.JSONParser;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

public class DZServiceImpl2
  implements DZDataInterface
{
  private static String CMBC_URL = ReadProperCMBC.getProperties("url");
  public static final PnbLogging LOG = PnbLogFactory.getLogger("CMBC_DZ_LOG");

  public DZInfoArray getDZData(String merchantnum, String dzdate) {
    List listall = new ArrayList();
    DZInfoArray dzInfoArray = new DZInfoArray();
    String termid = ReadProperCMBC.getProperties("termid_"+merchantnum);
    if (termid == null) {
      dzInfoArray.setRc("-1");
      dzInfoArray.setDzInfo(new DZInfo[0]);
      dzInfoArray.setErrormsg("终端号未配置");
      LOG.addError("终端号未配置");
      return dzInfoArray;
    }
    termid = termid.trim();
    String[] termids = termid.split(",");
    if ((termids == null) || (termids.length == 0)) {
      dzInfoArray.setRc("-1");
      dzInfoArray.setDzInfo(new DZInfo[0]);
      dzInfoArray.setErrormsg("终端号未配置");
      LOG.addError("终端号未配置02");
      return dzInfoArray;
    }
    for (int i = 0; i < termids.length; i++) {
      List list = getList(dzdate, termids[i], "1001", "01");
      if ((list != null) && (list.size() > 0)) {
        listall.addAll(list);
      }
    }
  /*  List list2 = getList(dzdate, "05000011", "1004", "");
    if ((list2 != null) && (list2.size() > 0))
      listall.addAll(list2);*/
    if ((listall == null) || (listall.size() == 0)) {
      dzInfoArray.setRc("-1");
      dzInfoArray.setDzInfo(new DZInfo[0]);
      dzInfoArray.setErrormsg("未获取到对账数据");
      LOG.addError("未获取到对账数据");
      return dzInfoArray;
    }
    DZInfo[] dzInfos = new DZInfo[listall.size()];
    for (int i = 0; i < listall.size(); i++) {
      dzInfos[i] = ((DZInfo)listall.get(i));
    }
    dzInfoArray.setRc("1");
    dzInfoArray.setDzInfo(dzInfos);
    dzInfoArray.setErrormsg("获取对账数据成功");
    LOG.addTrace("获取对账数据条数:" + listall.size());
    return dzInfoArray;
  }

  private static List<DZInfo> getList(String dzdate, String terminalno, String messageType, String queryType) {
    List list = new ArrayList();
    try {
      DZDataInPut dzDataInPut = new DZDataInPut();
      dzDataInPut.setTerminalNo(terminalno);
      dzDataInPut.setTerminalDate(dzdate);
      dzDataInPut.setMessageType(messageType);
      dzDataInPut.setQueryType(queryType);
      HttpClient httpClient = new HttpClient();

      PostMethod post = new PostMethod(CMBC_URL);
      String content = JSONParser.fromObject(dzDataInPut);
      if (LOG.isTrace()) LOG.addTrace("Send " + messageType + " HTTP POST requset:" + content);

      RequestEntity requestEntity = new StringRequestEntity(content, "application/json", "UTF-8");
      post.setRequestEntity(requestEntity);
      post.releaseConnection();

      int result = httpClient.executeMethod(post);
      if (LOG.isTrace()) LOG.addTrace("Response " + messageType + " status code:" + result);
      String resbody = post.getResponseBodyAsString();
      resbody = resbody.replaceAll("CenterDate", "centerDate");
      if (LOG.isTrace()) LOG.addTrace("Response " + messageType + " body:" + resbody);
      DZDataOutPut dzDataOutPut = (DZDataOutPut)JSONParser.toObject(resbody, DZDataOutPut.class);
      if (dzDataOutPut != null) {
        TradeDetail[] tradeDetails = dzDataOutPut.getTradeDetail();
        String termid = dzDataOutPut.getTerminalNo();
        if (tradeDetails == null)
          return null;
        for (int i = 0; i < tradeDetails.length; i++) {
          DZInfo dzInfo = new DZInfo();
          dzInfo.setPayFlowNO(tradeDetails[i].getHisWater());
          String tradeType = tradeDetails[i].getTradeType();
          dzInfo.setTranCode(tradeType);
          String balanceMoney = tradeDetails[i].getBalanceMoney();
          if (!StringHelper.isEmpty(balanceMoney))
            dzInfo.setAmount(Float.valueOf(balanceMoney));
          else
            dzInfo.setAmount(Float.valueOf("0"));
          dzInfo.setTermId(termid);
          dzInfo.setTranCardNum(tradeDetails[i].getAccountNo());
          dzInfo.setBatchNum(tradeDetails[i].getBatchNo());
          dzInfo.setTraceNum(tradeDetails[i].getTerminal_water());
          String TrDateTime = tradeDetails[i].getTradetime();
          
          
          dzInfo.setTrDateTime(DateUtil.stringFormat(TrDateTime, "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss"));
          String referNum = tradeDetails[i].getReferNo();
        /*  if (referNum.equals("161110163845")) {
            System.out.println();
          }*/
         /* if (StringHelper.isEmpty(tradeType))
            referNum = "T" + referNum;
          else if ("S2".equalsIgnoreCase(tradeType))
            referNum = "Z" + referNum;*/
          dzInfo.setReferNum(referNum);
          list.add(dzInfo);
        }
      }
    } catch (HttpException e) {
      LOG.addError(e);
      return null;
    } catch (IOException e) {
      LOG.addError(e);
      return null;
    }

    return list;
  }

  public static void main(String[] args)
  {
    DZInfoArray rtn = new DZServiceImpl2().getDZData("305420180623007", "20180102");
    if (rtn != null) {
      DZInfo[] ds = rtn.getDzInfo();
      if ((ds != null) && (ds.length > 0))
        for (int j = 0; j < ds.length; j++)
          System.out.println(ds[j]);
    }
  }
}